package com.ocom.webapi.config.aspect;

import com.alibaba.fastjson.JSONObject;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Locale;

@Aspect
@Component
public class ServiceAspect {

    private Logger log = LoggerFactory.getLogger(ServiceAspect.class);

    //切入点
    @Around("execution(* com.ocom.webapi.controller.*.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {

        // 类名
        String className = pjp.getTarget().getClass().getName();
        //方法名
        String methodName = pjp.getSignature().getName();


        Object[] args = pjp.getArgs();

        JSONObject jsonObject= null;
        try {
            for (int i = 0; i < args.length; i++) {
                Object object=args[i];
                if (object instanceof HttpServletRequest || object instanceof HttpServletResponse) {
                    continue;
                }
                jsonObject=JSONObject.parseObject(JSONObject.toJSONString(object));
            }
        }catch (Exception exception){

        }


        try {
            YoCiUser yoCiUser =  SecurityUtils.getYoCiUser();
            if(jsonObject!=null && yoCiUser!=null
            && !className.endsWith("OcomTechController")
            && !className.endsWith("FinancialController")
            && !className.endsWith("CompanyController")
            && !methodName.endsWith("role")
            && !className.endsWith("AppConfigController")
            && !className.endsWith("PayChannelController")
            && !className.endsWith("ConfigurePageController")
            && !methodName.contains("companyConfig")
            && (yoCiUser.getRole()==11l &&  !className.endsWith("ConsumeDeviceController")
                    &&  !className.endsWith("ConsumeDeviceController")
                    &&  !className.endsWith("AccessController")
                    &&  !className.endsWith("FlowConfigController")
                    &&  !methodName.endsWith("setConsDeviceMealConfig")
            )

            ){

                if(jsonObject.getLong("comId")!=null && jsonObject.getLong("comId")!=0l){
                    if(!jsonObject.getLong("comId").equals(yoCiUser.getComId())){
                        return ResultUtil.error(ResultEnum.Err_4100);
                    }
                }
            }
        }catch (Exception e){

        }


        Object re = pjp.proceed(args);

        try {
            YoCiUser yoCiUser =new YoCiUser("未知",new ArrayList<>(),new ArrayList<>());
            yoCiUser = SecurityUtils.getYoCiUser();
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            if(yoCiUser!=null && !methodName.toLowerCase(Locale.ROOT).contains("onshow") && !methodName.toLowerCase(Locale.ROOT).contains("import")
            && !request.getRequestURI().toLowerCase(Locale.ROOT).contains("onshow")
            && !request.getRequestURI().toLowerCase(Locale.ROOT).contains("list")
            && !request.getRequestURI().toLowerCase(Locale.ROOT).contains("import")
            && !request.getRequestURI().toLowerCase(Locale.ROOT).contains("template")
            && !request.getRequestURI().toLowerCase(Locale.ROOT).contains("template")
            && !request.getRequestURI().toLowerCase(Locale.ROOT).endsWith("info")
            && !methodName.equals("mainPagemenuPath")
            ){
                log.info("【系统日志】【webapi】管理员【{}】ip【{}】控制器【{}】方法【{}】参数【{}】返回【{}】",yoCiUser.getId(),request.getHeader("remoteip"),className,methodName,jsonObject,re);
            }
        }catch (Exception e){

        }
        return re;
    }
}
